<template>
  <div class="ticket-container">
    <edu-page-tabs :tab-list="xTabs" />
    <page-header content="活动详情" />
    <el-row type="flex" justify="start" class="ticket-header">
      <div class="icon"><span class="edu-icon-yingxiaozhongxin-huodong" /></div>
      <div class="ticket-info">
        <el-row type="flex" align="middle" justify="start" class="item">
          <div class="title">活动名称：</div>
          <div class="content">{{ ticketInfo.title }} <!--<span class="edu-icon-bianji" />--></div>
        </el-row>
        <el-row type="flex" align="middle" justify="start" class="item">
          <div class="title">活动期号：</div>
          <div class="content">{{ ticketInfo.issue_no }}</div>
        </el-row>
        <el-row type="flex" align="middle" justify="start" class="item">
          <div class="title">目标用户：</div>
          <div class="content">
            <target-user-type :row="ticketInfo" />
          </div>
        </el-row>
        <!--<el-row type="flex" align="middle" justify="start" class="item">-->
        <!--  <div class="title">活动时间：</div>-->
        <!--  <div class="content">2023-03-21 12:00 至 07-21 12:00</div>-->
        <!--</el-row>-->
      </div>
    </el-row>
    <el-divider />
    <div class="row">
      <el-row type="flex" justify="space-between" align="middle" class="title-row">
        <div>
          <span class="title-line" />
          <span class="title">活动券详情</span>
        </div>
        <div class="action">
          <el-button
            type="primary"
            size="mini"
            @click="createdTicketCoupon"
          > 新建活动券
          </el-button>
        </div>
      </el-row>
      <!-- 表格 start-->
      <edu-page-table
        :doactions="doactions"
        :checkbox-item="false"
        :custom-components="customComponents"
      />
    </div>
    <!--新建活动券 start-->
    <created-dialog
      v-if="createdDialogVisible"
      :visible.sync="createdDialogVisible"
      :ticket-info="ticketInfo"
      @reload="loadTableData"
    />
    <!--新建活动券 end-->
    <!--编辑活动券 start-->
    <updated-dialog
      v-if="updatedDialogVisible"
      :visible.sync="updatedDialogVisible"
      :ticket-info="ticketInfo"
      :data-info="currentRow"
      @reload="loadTableData"
    />
    <!--编辑活动券 end-->
  </div>
</template>

<script>
import EduPageTabs from '@public/template/tabs/page_tabs.vue';
import PageHeader from '@/components/page-header';
import EduPageTable from '@public/template/page_list/table.vue';
import TargetUserType from '../components/targetUserType'
import CreatedDialog from './CreatedDialog'
import UpdatedDialog from './UpdatedDialog'
import CouponType from './components/couponType'
import IsReceive from './components/isReceive'
import CouponInfo from './components/couponInfo'
import DeriveTime from './components/deriveTime'
import Effective from './components/effective'

export default {
  name: 'Manage',
  components: {
    EduPageTabs,
    PageHeader,
    EduPageTable,
    TargetUserType,
    CreatedDialog,
    UpdatedDialog
  },
  data() {
    return {
      xUri: '/admin/market/ticket/' + this.$route.params.id + '/coupon/list',
      xParams: {},
      xTabs: [
        {
          title: '活动券',
          uri: '/market/ticket/list',
          meta: {
            permission: 'market.ticket.list'
          }
        },
        {
          title: '活动详情',
          uri: '/market/ticket/' + this.$route.params.id + '/manage',
          meta: {
            permission: 'market.ticket.details'
          }
        }
      ],
      doactions: [
        {
          title: '编辑',
          meta: { permission: 'market.ticket.edit' },
          callback: this.edit,
          display({ row }) {
            return row.derive_number === 0
          }
        },
        {
          title: '短信通知',
          meta: { permission: 'market.ticket.edit' },
          callback: this.sendNotify
        },
        { title: '删除', meta: { permission: 'market.ticket.delete' }, callback: this.delete }
      ],
      customComponents: {
        coupon_type: CouponType,
        is_receive: IsReceive,
        coupon_info: CouponInfo,
        derive_time: DeriveTime,
        effective: Effective
      },
      // 获取数据
      ticketInfo: {
        title: '-',
        issue_no: '-',
        target_user_type: 0
      },
      // 新建活动券弹出层显隐性
      createdDialogVisible: false,
      // 编辑活动券弹出层显隐性
      updatedDialogVisible: false,
      // 当前编辑的行数据
      currentRow: {}
    }
  },
  computed: {
    ticketId() {
      return this.$route.params.id
    }
  },
  mounted() {
    this.$store.dispatch('setLoading', 0);
  },
  created() {
    this.loadTicketInfo()
    this.loadTableData()
  },
  methods: {
    // 加载活动详情
    loadTicketInfo() {
      this.$http.get(
          '/admin/market/ticket/' + this.ticketId + '/info',
          {},
          ({ data }) => {
            this.ticketInfo = data
          },
          _ => {
          }
      )
    },
    // 加载活动券表格数据
    loadTableData() {
      this.$store.dispatch('pageList/page', {
        uri: this.xUri,
        params: {
          ...this.xParams
        }
      })
    },
    // 新建活动券
    createdTicketCoupon() {
      this.createdDialogVisible = true
    },
    // 编辑
    edit({ row }) {
      this.currentRow = row
      this.updatedDialogVisible = true
    },
    // 删除
    delete({ row }) {
      this.$box
          .confirm('确认删除吗')
          .then(() => {
            this.$http.delete(
                '/admin/market/ticket/' + row.ticket_id + '/coupon/' + row.id,
                {},
                response => {
                  this.$message.success(response.msg, _ => {
                    this.loadTableData()
                  });
                },
                error => {
                  this.$message.error(error.msg);
                }
            );
          })
          .catch(() => {
          });
    },
    sendNotify({ row }) {
      this.$box
          .confirm('确定给符合活动条件的对象发送短信通知吗？')
          .then(() => {
            this.$http.post(
                '/admin/market/ticket/' + row.ticket_id + '/coupon/' + row.id + '/send/notify',
                {},
                response => {
                  this.$message.success(response.msg);
                },
                error => {
                  this.$message.error(error.msg);
                }
            );
          })
          .catch(() => {
          });
    }
  }
}
</script>

<style scoped lang="scss">
@import "@/styles/mixin.scss";

.ticket-container {
  .row {
    .title-row {
      margin-bottom: 10px;
      @include titleLine;

      .action {
        margin-right: 32px;
      }
    }
  }

  .ticket-header {
    padding: 20px 20px 0 20px;

    .icon {
      width: 120px;
      height: 120px;
      background: #F6F9FF;
      border-radius: 4px;
      text-align: center;
      vertical-align: middle;
      line-height: 120px;
      margin-right: 20px;

      span {
        font-size: 44px;
        color: #5191FF;
      }
    }

    .ticket-info {
      .item {
        font-size: 14px;
        font-weight: 400;
        color: #606266;
        line-height: 2;

        &:first-child {
          font-size: 15px;
          font-weight: bold;
          color: #303133;
          line-height: 2.4;

          .edu-icon-bianji {
            color: #5193FF;
          }
        }
      }
    }
  }
}
</style>
